﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace BlackJack
{
    public class Player
    {
        #region Private
        private double m_bank;
        private bool m_split;
        private bool m_double;
        private BlackJackHand[] m_hands;
        private int m_numberOfHands;
        private int m_currentHandIndex = 0;

        private const int DEFAULTBANKAMOUNT = 5000;
        private const int DEFAULTBETVALUE = 100;
        private const int DEFAULTNOOFHANDS = 1; 

        #endregion

        #region Properties

        public bool SplitCard
        {
            get { return m_split; }            
        }

        public bool DoubleHand
        {
            get { return m_double; }
        }

        #endregion

        #region Method
        public Player()
            : this(DEFAULTBANKAMOUNT)
        { }
        
        public Player(int initialBankAmount)
        {
            m_bank = initialBankAmount;
            m_hands = new BlackJackHand[2];
            m_hands[0] = new BlackJackHand();
            m_hands[1] = new BlackJackHand();
            m_numberOfHands = DEFAULTNOOFHANDS;
            m_split = false;
            m_double = false;        
        }

        public void Double()
        {
            //check if the hand has 2 cards and total value is less than equal to 10
            if ((m_hands[0].Total() <= 11 || m_hands[0].IsSoft()) && m_hands[0].Cards.Length == 2)
            {
                //double the bet value.
                m_bank -= m_hands[0].Bet;
                m_hands[0].Bet *= 2;
                m_double = true;
            }
        }

        public bool Split()
        {
            //make one hand two if the cards are the same
            if (m_hands[m_numberOfHands - 1].Cards[0].Equals(m_hands[m_numberOfHands].Cards[1]) && m_hands[m_numberOfHands-1].Cards.Length == 2)
            {
                m_hands[1].Add(m_hands[0].Cards[1]);
                m_hands[0].RemoveAt(1);
                m_hands[1].Bet = m_hands[0].Bet;
                m_numberOfHands++;
                m_split = true;               
            }
            return m_split;
        }

        public void Won(BlackJackHand hand)
        {
            m_bank += 2 * hand.Bet;
        }

        public void Push(BlackJackHand hand)
        {
            m_bank += hand.Bet;
        }

        public void Blackjack(BlackJackHand hand)
        {
            m_bank = hand.Bet + (hand.Bet * 1.5);
        }

        public void Reset()
        {
            m_hands = new BlackJackHand[2];
            m_hands[0] = new BlackJackHand();
            m_hands[1] = new BlackJackHand();

            m_numberOfHands = 1;
            m_currentHandIndex = 0;
        }

        public void SetBet()
        {
            SetBet(DEFAULTBETVALUE);
        }

        public void SetBet(double value)
        {
            m_hands[0].Bet = value;
            m_bank -= value;
        }

        public double TotalBet()
        {
            double total = 0.0;
            foreach(BlackJackHand h in m_hands)
            {
                total += h.Bet;
            }
            return total;
        }

        public BlackJackHand CurrentHand()
        {
            return m_hands[m_currentHandIndex];
        }

        public void NextHand()
        {
            m_currentHandIndex++;
        }

        public bool IsLastHand()
        {
            return (m_currentHandIndex == m_numberOfHands);
        }

        #endregion
    }
}
